.PHONY: install_tools lint dev yarn_dependencies ui server run

#本地目录配置，使用docker打包时需要修改
#项目目录
WK_PATH=/archer/Mega/code/tidb-dashboard
#YARN缓存，映射到本地减少重复下载
YARN_PATH=/home/zw/.cache/yarn/v6
#go缓存，映射到本地减少重复下载
GO_PATH=/archer/Tmp/go

DOCKER_WK_PATH=/go/
DOCKER_YARN_PATH=/usr/local/share/.cache/yarn/v6
DOCKER_GO_PATH=/home/go

CONTAINER=tidb-dashboard-builder


DASHBOARD_PKG := github.com/pingcap-incubator/tidb-dashboard

BUILD_TAGS ?=

LDFLAGS ?=

ifeq ($(UI),1)
	BUILD_TAGS += ui_server
endif

LDFLAGS += -X "$(DASHBOARD_PKG)/pkg/utils/version.InternalVersion=$(shell grep -v '^\#' ./release-version)"
LDFLAGS += -X "$(DASHBOARD_PKG)/pkg/utils/version.Standalone=Yes"
LDFLAGS += -X "$(DASHBOARD_PKG)/pkg/utils/version.PDVersion=N/A"
LDFLAGS += -X "$(DASHBOARD_PKG)/pkg/utils/version.BuildTime=$(shell date -u '+%Y-%m-%d %I:%M:%S')"
LDFLAGS += -X "$(DASHBOARD_PKG)/pkg/utils/version.BuildGitHash=$(shell git rev-parse HEAD)"

default: server

install_tools:
	scripts/install_go_tools.sh

lint:
	scripts/lint.sh

dev: lint default

yarn_dependencies: install_tools
	cd ui &&\
	yarn install --frozen-lockfile

ui: yarn_dependencies
	cd ui &&\
	yarn build

server: install_tools
	scripts/generate_swagger_spec.sh
ifeq ($(UI),1)
	scripts/embed_ui_assets.sh
endif
	go build -o bin/tidb-dashboard -ldflags '$(LDFLAGS)' -tags "${BUILD_TAGS}" cmd/tidb-dashboard/main.go

run:
	bin/tidb-dashboard --debug --experimental

#execute once to create docker image
docker-img:
	docker build --rm -t $(CONTAINER) -f Dockerfile.dev .

#build project in docker, slow in first time for download js package
docker-build:
	docker run --rm -it  \
    -v $(WK_PATH):$(DOCKER_WK_PATH) \
    -v $(YARN_PATH):$(DOCKER_YARN_PATH) \
    -v $(GO_PATH):$(DOCKER_GO_PATH) \
    -e GOPROXY="https://goproxy.cn,https://mirrors.aliyun.com/goproxy/,direct" \
    $(CONTAINER) \
    bash -c "make ui && UI=1 make"